package com.atmilan.quartz.utils.source;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.atmilan.quartz.model.entity.jky.JkyProductApi;
import com.atmilan.quartz.model.entity.jky.JkyProductSkuApi;
import com.atmilan.quartz.utils.LogsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atmilan.quartz.constant.Constants;
import com.atmilan.quartz.constant.enums.BarCodeEnum;
import com.atmilan.quartz.constant.enums.CategoryTypeEnum;
import com.atmilan.quartz.constant.enums.ProductImageEnum;
import com.atmilan.quartz.constant.enums.ProductTypeEnum;
import com.atmilan.quartz.constant.enums.StockAreaTempEnum;
import com.atmilan.quartz.model.dto.crmeb.StoreProductCrmebRequest;
import com.atmilan.quartz.model.entity.buyer.Supplier;
import com.atmilan.quartz.model.entity.crmeb.CategoryMatch;
import com.atmilan.quartz.model.entity.crmeb.ProductImage;
import com.atmilan.quartz.model.entity.crmeb.StoreBrand;
import com.atmilan.quartz.model.entity.crmeb.StoreProductAttr;
import com.atmilan.quartz.model.entity.jky.JkyProduct;
import com.atmilan.quartz.model.entity.jky.JkyProductSku;
import com.atmilan.quartz.service.ICategoryMatchService;
import com.atmilan.quartz.service.IProductImageService;
import com.atmilan.quartz.service.IStoreBrandService;
import com.atmilan.quartz.service.ISupplierService;
import com.atmilan.quartz.sync.push.crmeb.domain.StoreProductAttrValueRequest;
import com.atmilan.quartz.sync.push.crmeb.enums.CrmebMerchantEnums;
import com.atmilan.quartz.utils.PriceUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.util.StringUtil;

/**
 * 处理源数据转换和计算相关信息
 * @Description: 
 * @author daigb
 * @date 2021-03-05
 */
@Service
public class SourceJkyProductUtil {
    @Autowired
    IStoreBrandService storeBrandService;
    @Autowired
    ICategoryMatchService categoryMatchService;
    @Autowired
    ISupplierService supplierService;
    @Autowired
    PriceUtil priceUtil;
    @Autowired
    IProductImageService productImageService;
    @Value("${aliyun.oss.oss-url}")
    private String ossUrl;

    @Autowired
    private LogsUtil logsUtil;

    /**
     * 构建DO对象
     * @Description:  
     * @author daigb 
     * @param @param product
     * @param @param productSkus
     * @param @return
     * @return StoreProductRequest
     * @throws
     */
    @SuppressWarnings("unchecked")
	public StoreProductCrmebRequest buildJkyProduct(JkyProduct product, List<JkyProductSku> productSkus, String warehouseCode) {
        // 主商品对象
        StoreProductCrmebRequest productRequest = null;
        try {
            // 成本价
            BigDecimal price = BigDecimal.ZERO;
            // 原价
            BigDecimal otPrice = BigDecimal.ZERO;
            // 童装品牌单独逻辑
            String goodsNo = product.getGoodsNo();
            Boolean isBonpoint = false;
            if("46".equals(product.getBrandId())) {
            	isBonpoint = true;
            	if(!product.getGoodsNo().contains(" ")) {
                    //日志；
                    logsUtil.insertSyncProductLogs(product.getGoodsNo(),
                            product.getGoodsName(),product.getSkuName(),product.getSkuBarcode(),
                            "童装品牌GoodsNo无空格",
                            "JKY",product.getCateName(),product.getBrandName(),0);
            		return null;
            	}
            	goodsNo = product.getGoodsNo().split(" ")[0];
            }
            // 发货地
            StockAreaTempEnum stockAreaTempEnum = StockAreaTempEnum.CN;
            // 过滤未上架的图片
            List<String> colors = new ArrayList<>();
            // 计算最低价
            for(JkyProductSku sku : productSkus) {
            	if(null == sku.getPrice()) {
                    //日志；
                    logsUtil.insertSyncProductLogs(product.getGoodsNo(),
                            product.getGoodsName(),sku.getSkuName(),sku.getSkuBarcode(),
                            warehouseCode + " " + sku.getWarehouseName()+"Price为空",
                            "JKY",product.getCateName(),product.getBrandName(),0);
            		continue;
            	}
            	if("001".equals(warehouseCode)) {
            		if(null == sku.getOtPrice()) {
            			continue;
            		}
        			price = price.compareTo(sku.getOtPrice()) == 1 || price.compareTo(BigDecimal.ZERO) == 0 ? sku.getOtPrice() : price;
            	} else if("066".equals(warehouseCode)) {
            		if(null == sku.getOtPrice()) {
            			continue;
            		}
        			price = price.compareTo(sku.getOtPrice()) == 1 || price.compareTo(BigDecimal.ZERO) == 0 ? sku.getOtPrice() : price;
            	} else if("075".equals(warehouseCode)) {
            		if(null == sku.getOtPrice()) {
            			continue;
            		}
        			price = price.compareTo(sku.getOtPrice()) == 1 || price.compareTo(BigDecimal.ZERO) == 0 ? sku.getOtPrice() : price;
            	} else if("086".equals(warehouseCode)) {
            		if(null == sku.getOtPrice()) {
            			continue;
            		}
        			price = price.compareTo(sku.getOtPrice()) == 1 || price.compareTo(BigDecimal.ZERO) == 0 ? sku.getOtPrice() : price;
            	} else if("025".equals(warehouseCode)) {
            		if(null == sku.getVipPrice()) {
                        //日志；
                        logsUtil.insertSyncProductLogs(product.getGoodsNo(),
                                product.getGoodsName(),sku.getSkuName(),sku.getSkuBarcode(),
                                warehouseCode + " " + sku.getWarehouseName()+"025VipPrice为空",
                                "JKY",product.getCateName(),product.getBrandName(),0);
            			continue;
            		}
            		price = price.compareTo(sku.getVipPrice()) == 1 || price.compareTo(BigDecimal.ZERO) == 0 ? sku.getVipPrice() : price;
                    // 售价是原价打七折
//            		BigDecimal skuPrice = new BigDecimal("0.7").multiply(sku.getPrice()).setScale(2, RoundingMode.UP);
//            		price = price.compareTo(skuPrice) == 1 || price.compareTo(BigDecimal.ZERO) == 0 ? skuPrice : price;
            	} else if("018".equals(warehouseCode)) {
            		if(null == sku.getMinPrice()) {
            			continue;
            		}
            		price = price.compareTo(sku.getMinPrice()) == 1 || price.compareTo(BigDecimal.ZERO) == 0 ? sku.getMinPrice() : price;
            	} else if("tfx".equals(warehouseCode)) {
            		if(null == sku.getMinPrice()) {

            			continue;
            		}
            		price = price.compareTo(sku.getMinPrice()) == 1 || price.compareTo(BigDecimal.ZERO) == 0 ? sku.getMinPrice() : price;
            	} else {
            		if(null == sku.getPrice()) {

            			continue;
            		}

            		price = price.compareTo(sku.getPrice()) == 1 || price.compareTo(BigDecimal.ZERO) == 0 ? sku.getPrice() : price;
            	}
//        		if(null == sku.getPrice()) {
//        			continue;
//        		}
                otPrice = otPrice.compareTo(sku.getPrice()) == 1 || otPrice.compareTo(BigDecimal.ZERO) == 0 ? sku.getPrice() : otPrice;
                
            	String skuName = sku.getSkuName().replaceAll("（", "").replaceAll("）", "").replaceAll("代购", "")
    			.replaceAll("意现", "").replaceAll("国现", "").replaceAll("在途", "").replaceAll("黑色", "")
    			.replaceAll("白色", "").replaceAll("黄色", "").replaceAll("红色", "").replaceAll("蓝色", "")
    			.replaceAll("绿色", "").replaceAll("米色", "").replaceAll("藏蓝色", "").replaceAll("深蓝色", "")
    			.replaceAll("灰色", "").replaceAll("粉色", "").replaceAll("驼色", "").replaceAll("中灰色", "")
    			.replaceAll("粉红", "").replaceAll("酒红色", "").replaceAll("深蓝", "")
    			;
                String colorName = isBonpoint ? product.getGoodsNo().split(" ")[1] : skuName.split(";")[0];
                colors.add(colorName);
            }
            if(price.compareTo(BigDecimal.ZERO) == 0) {

                logsUtil.insertSyncProductLogs(product.getGoodsNo(),
                        product.getGoodsName(),product.getSkuName(),product.getSkuBarcode(),
                        warehouseCode + " " + "price为0",
                        "JKY",product.getCateName(),product.getBrandName(),0);

            	return null;
            }
            
            // 主图
            String mainImage = "";
			List<ProductImage> productImages = new ArrayList<>();
			// 淘分销取商品自带图片
			if("tfx".equals(warehouseCode)) {
				JSONArray imgUrlList = JSONArray.parseArray(product.getImgUrlList());
				for(int i=0; i<imgUrlList.size(); i++) {
					JSONObject imgJson = imgUrlList.getJSONObject(i).getJSONObject("imgUrl");
					String img = imgJson.getString("pic400x400");
					
					ProductImage productImage = new ProductImage();
					productImage.setIsPublic(true);
					productImage.setSort(i + "");
					productImage.setType(ProductImageEnum.GWPP1);
					productImage.setUrl(img);
					productImages.add(productImage);
				}
			} else {
				productImages = productImageService.list(Wrappers.<ProductImage>lambdaQuery()
	                    .eq(ProductImage :: getFolderName, goodsNo)
	                    .in(ProductImage :: getColor, colors)
	                    .orderByAsc(ProductImage :: getTypeSort, ProductImage :: getColor, ProductImage :: getSort)
	                    );
			}

            // 轮播图
            JSONArray gallery = new JSONArray();
            // 各种类型图片归类
            Map<String, List<String>> imageGroup = new HashMap<>();
            // 轮播归类图
//            Map<String, JSONArray> galleryMap = new HashMap<>();
            // sku主图
            Map<String, String> productImgMap = new HashMap<>();
            // 无图直接返回
            if(CollectionUtils.isEmpty(productImages)) {
                //日志；
                logsUtil.insertSyncProductLogs(product.getGoodsNo(),
                        product.getGoodsName(), product.getSkuName(), product.getSkuBarcode(),
                        warehouseCode + " " + "没有相关图片productImages",
                        "JKY", product.getCateName(),product.getBrandName(),0);
                return null;
            }
//            productImgMap = productImages.stream().collect(Collectors.toMap(ProductImage :: getColor, a -> (ossUrl + Constants.UPLOAD_FILE_WAREHOUSE_DIR + a.getUrl()), (v1,v2) -> v1));
//            String firstSort = "1";
            if(!"tfx".equals(warehouseCode)) {
            	for(ProductImage img : productImages) {
            		// 归类图片存储
            		if(null == imageGroup.get(img.getType().getName())) {
            			List<String> imgList = new ArrayList<>();
            			imgList.add(ossUrl + Constants.UPLOAD_FILE_WAREHOUSE_DIR + img.getUrl());
            			imageGroup.put(img.getType().getName(), imgList);
            		} else {
            			List<String> imgList = imageGroup.get(img.getType().getName());
            			imgList.add(ossUrl + Constants.UPLOAD_FILE_WAREHOUSE_DIR + img.getUrl());
            		}
            		// 公域当主图
            		if(StringUtil.isEmpty(mainImage) && img.getIsPublic()) {
            			// firstSort = img.getSort();
            			mainImage = ossUrl + Constants.UPLOAD_FILE_WAREHOUSE_DIR + img.getUrl();
            		}
            		// 取每种类型的第一张做轮播 
            		if("1".equals(img.getSort()) && img.getIsPublic()) {
            			gallery.add(ossUrl + Constants.UPLOAD_FILE_WAREHOUSE_DIR + img.getUrl());
            			if(null == productImgMap.get(img.getColor())) {
            				productImgMap.put(img.getColor(), ossUrl + Constants.UPLOAD_FILE_WAREHOUSE_DIR + img.getUrl());
            			}
            		} /* else {
					JSONArray galleryTemp = null == galleryMap.get(img.getColor()) ? new JSONArray() : galleryMap.get(img.getColor());
					galleryTemp.add(ossUrl + Constants.UPLOAD_FILE_WAREHOUSE_DIR + img.getUrl());
					
					galleryMap.put(img.getColor(), galleryTemp);
					}*/
            	}
            } else {
            	mainImage = productImages.get(0).getUrl();
            	for(ProductImage img : productImages) {
            		gallery.add(img.getUrl());
            	}
            }
            // 无图直接返回
            if(StringUtils.isBlank(mainImage)) {
                //日志；
                logsUtil.insertSyncProductLogs(product.getGoodsNo(),
                        product.getGoodsName(), product.getSkuName(), product.getSkuBarcode(),
                        warehouseCode + " " + "没有相关图片mainImage",
                        "JKY", product.getCateName(),product.getBrandName(),0);
                return null;
            }
//            galleryMap.forEach((k, v) -> {
//            	gallery.addAll(v);
//            });

            // 品牌对应
            StoreBrand buyerStoreBrand = null;
            if(StringUtil.isEmpty(product.getBrandId())) {
            	buyerStoreBrand = storeBrandService.getOne(Wrappers.<StoreBrand>lambdaQuery()
            			.eq(StoreBrand :: getEnName, product.getBrandName())
            			.eq(StoreBrand :: getType, ProductTypeEnum.JKY)
            			.last("LIMIT 1")
            			);
            } else {
            	buyerStoreBrand = storeBrandService.getOne(Wrappers.<StoreBrand>lambdaQuery()
                        .eq(StoreBrand :: getOtherId, product.getBrandId())
                        .eq(StoreBrand :: getType, ProductTypeEnum.JKY)
                        .last("LIMIT 1")
                        );
            }
            storeBrandService.unMatchBrand(buyerStoreBrand, product.getBrandId(),product.getBrandName(),ProductTypeEnum.JKY);
            StoreBrand storeBrand = null;
            if(null != buyerStoreBrand) {
                if(null != buyerStoreBrand.getSuperId()) {
                    storeBrand = storeBrandService.getById(buyerStoreBrand.getSuperId());
                } else {
                    storeBrand = storeBrandService.getById(5429);
                }
            } else {
                storeBrand = storeBrandService.getById(5429);
            }
            
            /***** 处理对象构建 *******/
            String barCode = BarCodeEnum.JKY.getCode() + product.getGoodsNo().toString();
            if("001".equals(warehouseCode)) {
            	stockAreaTempEnum = StockAreaTempEnum.CN;
            } else if("066".equals(warehouseCode)) {
            	stockAreaTempEnum = StockAreaTempEnum.CN;
            } else if("075".equals(warehouseCode)) {
            	stockAreaTempEnum = StockAreaTempEnum.CN;
            } else if("086".equals(warehouseCode)) {
            	stockAreaTempEnum = StockAreaTempEnum.CN;
            } else if ("004".equals(warehouseCode)) {
            	stockAreaTempEnum = StockAreaTempEnum.EUR;
            } else if ("KCTBXN".equals(warehouseCode)) {
            	stockAreaTempEnum = StockAreaTempEnum.EUR;
            }  else if ("ZTXHC".equals(warehouseCode)) {
                stockAreaTempEnum = StockAreaTempEnum.HK;
            }else if ("025".equals(warehouseCode)) {
            	stockAreaTempEnum = StockAreaTempEnum.EUR;
            } else if ("018".equals(warehouseCode)) {
            	stockAreaTempEnum = StockAreaTempEnum.IT;
            } else if ("tfx".equals(warehouseCode)) {
            	stockAreaTempEnum = StockAreaTempEnum.EUR;
            }
            barCode += "-" + warehouseCode;
            if(!"tfx".equals(warehouseCode)) {
            	barCode += "-" + product.getColorCode();
            }
            List<StoreProductAttr> attrList = new ArrayList<StoreProductAttr>();
            List<StoreProductAttrValueRequest> attrValueList = new ArrayList<StoreProductAttrValueRequest>();
            // 规格
            StoreProductAttr storeProductAttr = new StoreProductAttr();
            storeProductAttr.setAttrName("尺寸");
            storeProductAttr.setType(0);
            storeProductAttr.setBarCode(barCode);
            // 颜色
            StoreProductAttr storeProductAttrColor = new StoreProductAttr();
            storeProductAttrColor.setAttrName("颜色");
            storeProductAttrColor.setType(0);
            storeProductAttrColor.setBarCode(BarCodeEnum.JKY.getCode() + product.getGoodsNo().toString());
            // 合并成suk
            List<String> colorValueList = new ArrayList<String>();
            List<String> valueList = new ArrayList<String>();
//            Pattern pattern = Pattern.compile("\\d+");
            int stock = 0;
            for(JkyProductSku sku : productSkus) {
            	String skuName = sku.getSkuName().replaceAll("（", "").replaceAll("）", "").replaceAll("代购", "")
            			.replaceAll("意现", "").replaceAll("国现", "").replaceAll("在途", "").replaceAll("黑色", "")
            			.replaceAll("白色", "").replaceAll("黄色", "").replaceAll("红色", "").replaceAll("蓝色", "")
            			.replaceAll("绿色", "").replaceAll("米色", "").replaceAll("藏蓝色", "").replaceAll("深蓝色", "")
            			.replaceAll("灰色", "").replaceAll("粉色", "").replaceAll("驼色", "").replaceAll("中灰色", "")
            			.replaceAll("粉红", "").replaceAll("酒红色", "").replaceAll("深蓝", "")
            			;
                String colorName = isBonpoint ? product.getGoodsNo().split(" ")[1] : skuName.split(";")[0];
                String skuSize = skuName.split(";").length > 1 ? skuName.split(";")[1] : skuName;
//                Matcher matcher = pattern.matcher(colorName);
//                while (matcher.find()) {
//                	colorName = matcher.group(0);
//                }
//                String skuImage = null != productImgMap.get(colorName) ? productImgMap.get(colorName) : mainImage;
                String skuImage = "tfx".equals(warehouseCode) ? mainImage : productImgMap.get(colorName);
                // 未匹配到图片的sku不下放
                if(StringUtil.isEmpty(skuImage)) {
                    //日志；
                    logsUtil.insertSyncProductLogs(product.getGoodsNo(),
                            product.getGoodsName(), product.getSkuName(), product.getSkuBarcode(),
                            warehouseCode + " " + "没有相关图片skuImage",
                            "JKY", product.getCateName(),product.getBrandName(),0);
                	continue;
                }

                // value对象
                StoreProductAttrValueRequest storeProductAttrValue = new StoreProductAttrValueRequest();
                storeProductAttrValue.setSourceSkuId(sku.getSkuBarcode());
                storeProductAttrValue.setSelfSourceSkuId(sku.getSkuBarcode());

                // 成本价
                BigDecimal skuPrice = BigDecimal.ZERO;
                // 原价
                BigDecimal skuOtPrice = BigDecimal.ZERO;
                if("001".equals(warehouseCode)) {
            		if(null == sku.getOtPrice()) {
            			skuPrice = null == sku.getPrice() ? skuPrice : sku.getPrice();
            		} else {
            			skuPrice = sku.getOtPrice();
            		}
            	} else if("066".equals(warehouseCode)) {
            		if(null == sku.getOtPrice()) {
            			skuPrice = null == sku.getPrice() ? skuPrice : sku.getPrice();
            		} else {
            			skuPrice = sku.getOtPrice();
            		}
            	} else if("075".equals(warehouseCode)) {
            		if(null == sku.getOtPrice()) {
            			skuPrice = null == sku.getPrice() ? skuPrice : sku.getPrice();
            		} else {
            			skuPrice = sku.getOtPrice();
            		}
            	} else if("086".equals(warehouseCode)) {
            		if(null == sku.getOtPrice()) {
            			skuPrice = null == sku.getPrice() ? skuPrice : sku.getPrice();
            		} else {
            			skuPrice = sku.getOtPrice();
            		}
            	} else if("025".equals(warehouseCode)) {
            		skuPrice = null == sku.getVipPrice() ? skuPrice : sku.getVipPrice();
//            		skuPrice = null == sku.getPrice() ? skuPrice : sku.getPrice();
            		// 025原价七折为售价
//            		skuPrice = new BigDecimal("0.7").multiply(skuPrice).setScale(2, RoundingMode.UP);
            	} else if("018".equals(warehouseCode)) {
            		skuPrice = null == sku.getMinPrice() ? skuPrice : sku.getMinPrice();
            	} else if("tfx".equals(warehouseCode)) {
            		skuPrice = null == sku.getMinPrice() ? skuPrice : sku.getMinPrice();
            	} else {
            		skuPrice = null == sku.getOtPrice() ? skuPrice : sku.getOtPrice();
            	}
            	if(null == skuPrice || skuPrice.compareTo(BigDecimal.ZERO) == 0) {
                    logsUtil.insertSyncProductLogs(product.getGoodsNo(),
                            product.getGoodsName(), product.getSkuName(), product.getSkuBarcode(),
                            warehouseCode + " " +  "skuPrice为空或者为0",
                            "JKY", product.getCateName(),product.getBrandName(),0);
            		continue;
            	}
            	skuOtPrice = sku.getPrice();
                
                LinkedHashMap<String, String> linkedMap = new LinkedHashMap<>();
                List<String> suk = new ArrayList<String>();
                linkedMap.put("颜色", "默认");
                if (!colorValueList.contains("默认")) {
                    colorValueList.add("默认");
                }
                suk.add("默认");
                linkedMap.put("尺寸", skuSize);
                if (!valueList.contains(skuSize)) {
                    valueList.add(skuSize);
                }
                suk.add(skuSize);
                storeProductAttrValue.setSuk(String.join(",", suk));
                storeProductAttrValue.setAttrValue(linkedMap);
        		int lockedQuantity = null == sku.getLockedQuantity() ? 0 : Integer.parseInt(sku.getLockedQuantity());
                storeProductAttrValue.setStock(Integer.parseInt(sku.getCurrentQuantity()) - lockedQuantity);
                storeProductAttrValue.setPrice(skuPrice);
                storeProductAttrValue.setImage(skuImage);
                storeProductAttrValue.setCost(skuPrice);
                storeProductAttrValue.setSourceCost(skuPrice);
                storeProductAttrValue.setBarCode(barCode);
                storeProductAttrValue.setOtPrice(priceUtil.rmb2EurStatic(skuOtPrice));
                storeProductAttrValue.setTempId(stockAreaTempEnum.getTempId());
                attrValueList.add(storeProductAttrValue);
                
                stock += storeProductAttrValue.getStock();
            }
            if(CollectionUtils.isEmpty(attrValueList)) {
            	return null;
            }
            // 已售罄
            if(0 >= stock) {
            	return null;
            }
            // attr对象
            if (CollectionUtils.isNotEmpty(colorValueList)) {
                storeProductAttrColor.setAttrValues(String.join(",", colorValueList));
                attrList.add(storeProductAttrColor);
            }
            if (CollectionUtils.isNotEmpty(valueList)) {
                storeProductAttr.setAttrValues(String.join(",", valueList));
                attrList.add(storeProductAttr);
            }
            
            productRequest = new StoreProductCrmebRequest();
//            productRequest.setExchangeRate(new BigDecimal(PriceUtil.DEFAULT_EXCHANGE_RATE_EUR));

            productRequest.setAttr(attrList);
            productRequest.setAttrValue(attrValueList);
            // 强制写一个 商户id 作为buyer商品标识标识
            productRequest.setMerId(CrmebMerchantEnums.CHENGPING.getMerchantId());
            productRequest.setContent(product.getGoodsDesc());
            productRequest.setKeyword(goodsNo);
            productRequest.setStoreInfo(product.getGoodsDesc());
            productRequest.setStoreName(product.getGoodsName().replaceAll("意大利代购", "").replaceAll("（", "").replaceAll("）", "").replaceAll("代购", "").replaceAll("在途", ""));
            if(null != storeBrand) {
                productRequest.setBrandId(storeBrand.getId());
                productRequest.setBrandName(storeBrand.getEnName());
            }
            productRequest.setPrice(price);
            productRequest.setOtPrice(priceUtil.rmb2EurStatic(otPrice));
            productRequest.setCost(price);
            productRequest.setStock(stock);
            productRequest.setSourceCost(price);
            productRequest.setPromoteFlag(false);
            productRequest.setErpPromFlag(true);
            productRequest.setTempId(stockAreaTempEnum.getTempId());
            
            // 供应商
            Supplier supplier = supplierService.getOne(Wrappers.<Supplier>lambdaQuery()
                    .eq(Supplier :: getBarcode, BarCodeEnum.JKY.getCode())
                    .last("LIMIT 1")
                    );
            if(null != supplier) {
                productRequest.setSuppliersId(supplier.getId());
                productRequest.setSuppliersName(supplier.getSuppliersName());
            }
            productRequest.setBarCode(barCode);
            // 分类
            CategoryMatch categoryMatch = categoryMatchService.getOne(Wrappers.<CategoryMatch>lambdaQuery()
                    .eq(CategoryMatch :: getType, CategoryTypeEnum.JKY)
                    .eq(CategoryMatch :: getOthCategoryId, product.getCateId())
                    .eq(!"tfx".equals(warehouseCode), CategoryMatch :: getOthCategoryType, product.getAbcCate())
                    .last("LIMIT 1")
                    );
            categoryMatchService.unMatchCategory(categoryMatch,product.getCateId(),product.getAbcCate(),null,CategoryTypeEnum.JKY);
            // 复制重量
            if(null != categoryMatch) {
            	CategoryMatch supCategoryMatch = categoryMatchService.getById(categoryMatch.getCategoryId());
            	categoryMatch.setWeight(Optional.ofNullable(supCategoryMatch).orElse(new CategoryMatch()).getWeight());
            }
            Integer categoryId = null != categoryMatch ? categoryMatch.getCategoryId() : 1859;
            productRequest.setCateId(categoryId);
            productRequest.setCateIds(Arrays.asList(categoryId));
            productRequest.setWeight(null != categoryMatch ? categoryMatch.getWeight() : BigDecimal.ONE);
            productRequest.setImage(StringUtil.isEmpty(mainImage) ? gallery.getString(0) : mainImage);
            if(gallery.size() > 0) {
                productRequest.setSliderImage(JSON.toJSONString(gallery));
            }
            productRequest.setIsShow(true);
            productRequest.setSpecType(true);
            productRequest.setIsEurope(ProductTypeEnum.JKY.getCode());
            productRequest.setUnitName("件");
            productRequest.setSeasonCode(product.getGoodsAlias());
            productRequest.setColorCode(product.getColorCode());
            productRequest.setLastPushTime(new Date());
            productRequest.setAddTime(Integer.parseInt(String.valueOf(System.currentTimeMillis() / 1000L)));
            // 图片视频归类存储
            productRequest.setGwpp1(null == imageGroup.get(ProductImageEnum.GWPP1.getName()) ? null : JSON.toJSONString(imageGroup.get(ProductImageEnum.GWPP1.getName())));
            productRequest.setGwmt1(null == imageGroup.get(ProductImageEnum.GWMT1.getName()) ? null : JSON.toJSONString(imageGroup.get(ProductImageEnum.GWMT1.getName())));
            productRequest.setSp1(null == imageGroup.get(ProductImageEnum.SP1.getName()) ? null : JSON.toJSONString(imageGroup.get(ProductImageEnum.SP1.getName())));
            productRequest.setSp(null == imageGroup.get(ProductImageEnum.SP.getName()) ? null : JSON.toJSONString(imageGroup.get(ProductImageEnum.SP.getName())));
            productRequest.setMt1(null == imageGroup.get(ProductImageEnum.MT1.getName()) ? null : JSON.toJSONString(imageGroup.get(ProductImageEnum.MT1.getName())));
            productRequest.setMt(null == imageGroup.get(ProductImageEnum.MT.getName()) ? null : JSON.toJSONString(imageGroup.get(ProductImageEnum.MT.getName())));
            productRequest.setMx1(null == imageGroup.get(ProductImageEnum.MX1.getName()) ? null : JSON.toJSONString(imageGroup.get(ProductImageEnum.MX1.getName())));
            productRequest.setSp2(null == imageGroup.get(ProductImageEnum.SP2.getName()) ? null : JSON.toJSONString(imageGroup.get(ProductImageEnum.SP2.getName())));
            productRequest.setSp3(null == imageGroup.get(ProductImageEnum.SP3.getName()) ? null : JSON.toJSONString(imageGroup.get(ProductImageEnum.SP3.getName())));
            BigDecimal exchangeRate = priceUtil.getMoneyExchange(supplier.getCurrencyId());
            productRequest.setExchangeRate(exchangeRate);

        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return productRequest;
    }

    /**
     * 构建DO对象
     * @Description:
     * @author daigb
     * @param @param product
     * @param @param productSkus
     * @param @return
     * @return StoreProductRequest
     * @throws
     */
    @SuppressWarnings("unchecked")
    public StoreProductCrmebRequest buildJkyProduct(JkyProductApi product, List<JkyProductSkuApi> productSkus, String warehouseCode) {
        // 主商品对象
        StoreProductCrmebRequest productRequest = null;
        try {
            // 成本价
            BigDecimal price = BigDecimal.ZERO;
            // 原价
            BigDecimal otPrice = BigDecimal.ZERO;
            // 童装品牌单独逻辑
            String goodsNo = product.getGoodsNo();
            Boolean isBonpoint = false;
            if("46".equals(product.getBrandId())) {
                isBonpoint = true;
                if(!product.getGoodsNo().contains(" ")) {
                    //日志；
                    logsUtil.insertSyncProductLogs(product.getGoodsNo(),
                            product.getGoodsName(),product.getSkuName(),product.getSkuBarcode(),
                            "童装品牌GoodsNo无空格",
                            "JKY",product.getCateName(),product.getBrandName(),0);
                    return null;
                }
                goodsNo = product.getGoodsNo().split(" ")[0];
            }
            // 发货地
            StockAreaTempEnum stockAreaTempEnum = StockAreaTempEnum.CN;
            // 过滤未上架的图片
            List<String> colors = new ArrayList<>();
            // 计算最低价
            for(JkyProductSkuApi sku : productSkus) {
                if(null == sku.getPrice()) {
                    //日志；
                    logsUtil.insertSyncProductLogs(product.getGoodsNo(),
                            product.getGoodsName(),sku.getSkuName(),sku.getSkuBarcode(),
                            warehouseCode + " " + sku.getWarehouseName()+"Price为空",
                            "JKY",product.getCateName(),product.getBrandName(),0);
                    continue;
                }
                if("001".equals(warehouseCode)) {
                    if(null == sku.getOtPrice()) {
                        continue;
                    }
                    price = price.compareTo(sku.getOtPrice()) == 1 || price.compareTo(BigDecimal.ZERO) == 0 ? sku.getOtPrice() : price;
                } else if("066".equals(warehouseCode)) {
                    if(null == sku.getOtPrice()) {
                        continue;
                    }
                    price = price.compareTo(sku.getOtPrice()) == 1 || price.compareTo(BigDecimal.ZERO) == 0 ? sku.getOtPrice() : price;
                } else if("075".equals(warehouseCode)) {
                    if(null == sku.getOtPrice()) {
                        continue;
                    }
                    price = price.compareTo(sku.getOtPrice()) == 1 || price.compareTo(BigDecimal.ZERO) == 0 ? sku.getOtPrice() : price;
                } else if("086".equals(warehouseCode)) {
                    if(null == sku.getOtPrice()) {
                        continue;
                    }
                    price = price.compareTo(sku.getOtPrice()) == 1 || price.compareTo(BigDecimal.ZERO) == 0 ? sku.getOtPrice() : price;
                } else if("025".equals(warehouseCode)) {
                    if(null == sku.getVipPrice()) {
                        //日志；
                        logsUtil.insertSyncProductLogs(product.getGoodsNo(),
                                product.getGoodsName(),sku.getSkuName(),sku.getSkuBarcode(),
                                warehouseCode + " " + sku.getWarehouseName()+"025VipPrice为空",
                                "JKY",product.getCateName(),product.getBrandName(),0);
                        continue;
                    }
                    price = price.compareTo(sku.getVipPrice()) == 1 || price.compareTo(BigDecimal.ZERO) == 0 ? sku.getVipPrice() : price;
                    // 售价是原价打七折
//            		BigDecimal skuPrice = new BigDecimal("0.7").multiply(sku.getPrice()).setScale(2, RoundingMode.UP);
//            		price = price.compareTo(skuPrice) == 1 || price.compareTo(BigDecimal.ZERO) == 0 ? skuPrice : price;
                } else if("018".equals(warehouseCode)) {
                    if(null == sku.getMinPrice()) {
                        continue;
                    }
                    price = price.compareTo(sku.getMinPrice()) == 1 || price.compareTo(BigDecimal.ZERO) == 0 ? sku.getMinPrice() : price;
                } else if("tfx".equals(warehouseCode)) {
                    if(null == sku.getMinPrice()) {

                        continue;
                    }
                    price = price.compareTo(sku.getMinPrice()) == 1 || price.compareTo(BigDecimal.ZERO) == 0 ? sku.getMinPrice() : price;
                } else {
                    if(null == sku.getPrice()) {

                        continue;
                    }

                    price = price.compareTo(sku.getPrice()) == 1 || price.compareTo(BigDecimal.ZERO) == 0 ? sku.getPrice() : price;
                }
//        		if(null == sku.getPrice()) {
//        			continue;
//        		}
                otPrice = otPrice.compareTo(sku.getPrice()) == 1 || otPrice.compareTo(BigDecimal.ZERO) == 0 ? sku.getPrice() : otPrice;

                String skuName = sku.getSkuName().replaceAll("（", "").replaceAll("）", "").replaceAll("代购", "")
                        .replaceAll("意现", "").replaceAll("国现", "").replaceAll("在途", "").replaceAll("黑色", "")
                        .replaceAll("白色", "").replaceAll("黄色", "").replaceAll("红色", "").replaceAll("蓝色", "")
                        .replaceAll("绿色", "").replaceAll("米色", "").replaceAll("藏蓝色", "").replaceAll("深蓝色", "")
                        .replaceAll("灰色", "").replaceAll("粉色", "").replaceAll("驼色", "").replaceAll("中灰色", "")
                        .replaceAll("粉红", "").replaceAll("酒红色", "").replaceAll("深蓝", "")
                        ;
                String colorName = isBonpoint ? product.getGoodsNo().split(" ")[1] : skuName.split(";")[0];
                colors.add(colorName);
            }
            if(price.compareTo(BigDecimal.ZERO) == 0) {

                logsUtil.insertSyncProductLogs(product.getGoodsNo(),
                        product.getGoodsName(),product.getSkuName(),product.getSkuBarcode(),
                        warehouseCode + " " + "price为0",
                        "JKY",product.getCateName(),product.getBrandName(),0);

                return null;
            }

            // 主图
            String mainImage = "";
            List<ProductImage> productImages = new ArrayList<>();
            // 淘分销取商品自带图片
            if("tfx".equals(warehouseCode)) {
                JSONArray imgUrlList = JSONArray.parseArray(product.getImgUrlList());
                for(int i=0; i<imgUrlList.size(); i++) {
                    JSONObject imgJson = imgUrlList.getJSONObject(i).getJSONObject("imgUrl");
                    String img = imgJson.getString("pic400x400");

                    ProductImage productImage = new ProductImage();
                    productImage.setIsPublic(true);
                    productImage.setSort(i + "");
                    productImage.setType(ProductImageEnum.GWPP1);
                    productImage.setUrl(img);
                    productImages.add(productImage);
                }
            } else {
                productImages = productImageService.list(Wrappers.<ProductImage>lambdaQuery()
                        .eq(ProductImage :: getFolderName, goodsNo)
                        .in(ProductImage :: getColor, colors)
                        .orderByAsc(ProductImage :: getTypeSort, ProductImage :: getColor, ProductImage :: getSort)
                );
            }

            // 轮播图
            JSONArray gallery = new JSONArray();
            // 各种类型图片归类
            Map<String, List<String>> imageGroup = new HashMap<>();
            // 轮播归类图
//            Map<String, JSONArray> galleryMap = new HashMap<>();
            // sku主图
            Map<String, String> productImgMap = new HashMap<>();
            // 无图直接返回
            if(CollectionUtils.isEmpty(productImages)) {
                //日志；
                logsUtil.insertSyncProductLogs(product.getGoodsNo(),
                        product.getGoodsName(), product.getSkuName(), product.getSkuBarcode(),
                        warehouseCode + " " + "没有相关图片productImages",
                        "JKY", product.getCateName(),product.getBrandName(),0);
                return null;
            }
//            productImgMap = productImages.stream().collect(Collectors.toMap(ProductImage :: getColor, a -> (ossUrl + Constants.UPLOAD_FILE_WAREHOUSE_DIR + a.getUrl()), (v1,v2) -> v1));
//            String firstSort = "1";
            if(!"tfx".equals(warehouseCode)) {
                for(ProductImage img : productImages) {
                    // 归类图片存储
                    if(null == imageGroup.get(img.getType().getName())) {
                        List<String> imgList = new ArrayList<>();
                        imgList.add(ossUrl + Constants.UPLOAD_FILE_WAREHOUSE_DIR + img.getUrl());
                        imageGroup.put(img.getType().getName(), imgList);
                    } else {
                        List<String> imgList = imageGroup.get(img.getType().getName());
                        imgList.add(ossUrl + Constants.UPLOAD_FILE_WAREHOUSE_DIR + img.getUrl());
                    }
                    // 公域当主图
                    if(StringUtil.isEmpty(mainImage) && img.getIsPublic()) {
                        // firstSort = img.getSort();
                        mainImage = ossUrl + Constants.UPLOAD_FILE_WAREHOUSE_DIR + img.getUrl();
                    }
                    // 取每种类型的第一张做轮播
                    if("1".equals(img.getSort()) && img.getIsPublic()) {
                        gallery.add(ossUrl + Constants.UPLOAD_FILE_WAREHOUSE_DIR + img.getUrl());
                        if(null == productImgMap.get(img.getColor())) {
                            productImgMap.put(img.getColor(), ossUrl + Constants.UPLOAD_FILE_WAREHOUSE_DIR + img.getUrl());
                        }
                    } /* else {
					JSONArray galleryTemp = null == galleryMap.get(img.getColor()) ? new JSONArray() : galleryMap.get(img.getColor());
					galleryTemp.add(ossUrl + Constants.UPLOAD_FILE_WAREHOUSE_DIR + img.getUrl());

					galleryMap.put(img.getColor(), galleryTemp);
					}*/
                }
            } else {
                mainImage = productImages.get(0).getUrl();
                for(ProductImage img : productImages) {
                    gallery.add(img.getUrl());
                }
            }
            // 无图直接返回
            if(StringUtils.isBlank(mainImage)) {
                //日志；
                logsUtil.insertSyncProductLogs(product.getGoodsNo(),
                        product.getGoodsName(), product.getSkuName(), product.getSkuBarcode(),
                        warehouseCode + " " + "没有相关图片mainImage",
                        "JKY", product.getCateName(),product.getBrandName(),0);
                return null;
            }
//            galleryMap.forEach((k, v) -> {
//            	gallery.addAll(v);
//            });

            // 品牌对应
            StoreBrand buyerStoreBrand = null;
            if(StringUtil.isEmpty(product.getBrandId())) {
                buyerStoreBrand = storeBrandService.getOne(Wrappers.<StoreBrand>lambdaQuery()
                        .eq(StoreBrand :: getEnName, product.getBrandName())
                        .eq(StoreBrand :: getType, ProductTypeEnum.JKY)
                        .last("LIMIT 1")
                );
            } else {
                buyerStoreBrand = storeBrandService.getOne(Wrappers.<StoreBrand>lambdaQuery()
                        .eq(StoreBrand :: getOtherId, product.getBrandId())
                        .eq(StoreBrand :: getType, ProductTypeEnum.JKY)
                        .last("LIMIT 1")
                );
            }
            storeBrandService.unMatchBrand(buyerStoreBrand, product.getBrandId(),product.getBrandName(),ProductTypeEnum.JKY);
            StoreBrand storeBrand = null;
            if(null != buyerStoreBrand) {
                if(null != buyerStoreBrand.getSuperId()) {
                    storeBrand = storeBrandService.getById(buyerStoreBrand.getSuperId());
                } else {
                    storeBrand = storeBrandService.getById(5429);
                }
            } else {
                storeBrand = storeBrandService.getById(5429);
            }

            /***** 处理对象构建 *******/
            String barCode = BarCodeEnum.JKY.getCode() + product.getGoodsNo().toString();
            if("001".equals(warehouseCode)) {
                stockAreaTempEnum = StockAreaTempEnum.CN;
            } else if("066".equals(warehouseCode)) {
                stockAreaTempEnum = StockAreaTempEnum.CN;
            } else if("075".equals(warehouseCode)) {
                stockAreaTempEnum = StockAreaTempEnum.CN;
            } else if("086".equals(warehouseCode)) {
                stockAreaTempEnum = StockAreaTempEnum.CN;
            } else if ("004".equals(warehouseCode)) {
                stockAreaTempEnum = StockAreaTempEnum.EUR;
            } else if ("KCTBXN".equals(warehouseCode)) {
                stockAreaTempEnum = StockAreaTempEnum.EUR;
            }  else if ("ZTXHC".equals(warehouseCode)) {
                stockAreaTempEnum = StockAreaTempEnum.HK;
            }else if ("025".equals(warehouseCode)) {
                stockAreaTempEnum = StockAreaTempEnum.EUR;
            } else if ("018".equals(warehouseCode)) {
                stockAreaTempEnum = StockAreaTempEnum.IT;
            } else if ("tfx".equals(warehouseCode)) {
                stockAreaTempEnum = StockAreaTempEnum.EUR;
            }
            barCode += "-" + warehouseCode;
            if(!"tfx".equals(warehouseCode)) {
                barCode += "-" + product.getColorCode();
            }
            List<StoreProductAttr> attrList = new ArrayList<StoreProductAttr>();
            List<StoreProductAttrValueRequest> attrValueList = new ArrayList<StoreProductAttrValueRequest>();
            // 规格
            StoreProductAttr storeProductAttr = new StoreProductAttr();
            storeProductAttr.setAttrName("尺寸");
            storeProductAttr.setType(0);
            storeProductAttr.setBarCode(barCode);
            // 颜色
            StoreProductAttr storeProductAttrColor = new StoreProductAttr();
            storeProductAttrColor.setAttrName("颜色");
            storeProductAttrColor.setType(0);
            storeProductAttrColor.setBarCode(BarCodeEnum.JKY.getCode() + product.getGoodsNo().toString());
            // 合并成suk
            List<String> colorValueList = new ArrayList<String>();
            List<String> valueList = new ArrayList<String>();
//            Pattern pattern = Pattern.compile("\\d+");
            int stock = 0;
            for(JkyProductSkuApi sku : productSkus) {
                String skuName = sku.getSkuName().replaceAll("（", "").replaceAll("）", "").replaceAll("代购", "")
                        .replaceAll("意现", "").replaceAll("国现", "").replaceAll("在途", "").replaceAll("黑色", "")
                        .replaceAll("白色", "").replaceAll("黄色", "").replaceAll("红色", "").replaceAll("蓝色", "")
                        .replaceAll("绿色", "").replaceAll("米色", "").replaceAll("藏蓝色", "").replaceAll("深蓝色", "")
                        .replaceAll("灰色", "").replaceAll("粉色", "").replaceAll("驼色", "").replaceAll("中灰色", "")
                        .replaceAll("粉红", "").replaceAll("酒红色", "").replaceAll("深蓝", "")
                        ;
                String colorName = isBonpoint ? product.getGoodsNo().split(" ")[1] : skuName.split(";")[0];
                String skuSize = skuName.split(";").length > 1 ? skuName.split(";")[1] : skuName;
//                Matcher matcher = pattern.matcher(colorName);
//                while (matcher.find()) {
//                	colorName = matcher.group(0);
//                }
//                String skuImage = null != productImgMap.get(colorName) ? productImgMap.get(colorName) : mainImage;
                String skuImage = "tfx".equals(warehouseCode) ? mainImage : productImgMap.get(colorName);
                // 未匹配到图片的sku不下放
                if(StringUtil.isEmpty(skuImage)) {
                    //日志；
                    logsUtil.insertSyncProductLogs(product.getGoodsNo(),
                            product.getGoodsName(), product.getSkuName(), product.getSkuBarcode(),
                            warehouseCode + " " + "没有相关图片skuImage",
                            "JKY", product.getCateName(),product.getBrandName(),0);
                    continue;
                }

                // value对象
                StoreProductAttrValueRequest storeProductAttrValue = new StoreProductAttrValueRequest();
                storeProductAttrValue.setSourceSkuId(sku.getSkuBarcode());
                storeProductAttrValue.setSelfSourceSkuId(sku.getSkuBarcode());

                // 成本价
                BigDecimal skuPrice = BigDecimal.ZERO;
                // 原价
                BigDecimal skuOtPrice = BigDecimal.ZERO;
                if("001".equals(warehouseCode)) {
                    if(null == sku.getOtPrice()) {
                        skuPrice = null == sku.getPrice() ? skuPrice : sku.getPrice();
                    } else {
                        skuPrice = sku.getOtPrice();
                    }
                } else if("066".equals(warehouseCode)) {
                    if(null == sku.getOtPrice()) {
                        skuPrice = null == sku.getPrice() ? skuPrice : sku.getPrice();
                    } else {
                        skuPrice = sku.getOtPrice();
                    }
                } else if("075".equals(warehouseCode)) {
                    if(null == sku.getOtPrice()) {
                        skuPrice = null == sku.getPrice() ? skuPrice : sku.getPrice();
                    } else {
                        skuPrice = sku.getOtPrice();
                    }
                } else if("086".equals(warehouseCode)) {
                    if(null == sku.getOtPrice()) {
                        skuPrice = null == sku.getPrice() ? skuPrice : sku.getPrice();
                    } else {
                        skuPrice = sku.getOtPrice();
                    }
                } else if("025".equals(warehouseCode)) {
                    skuPrice = null == sku.getVipPrice() ? skuPrice : sku.getVipPrice();
//            		skuPrice = null == sku.getPrice() ? skuPrice : sku.getPrice();
                    // 025原价七折为售价
//            		skuPrice = new BigDecimal("0.7").multiply(skuPrice).setScale(2, RoundingMode.UP);
                } else if("018".equals(warehouseCode)) {
                    skuPrice = null == sku.getMinPrice() ? skuPrice : sku.getMinPrice();
                } else if("tfx".equals(warehouseCode)) {
                    skuPrice = null == sku.getMinPrice() ? skuPrice : sku.getMinPrice();
                } else {
                    skuPrice = null == sku.getOtPrice() ? skuPrice : sku.getOtPrice();
                }
                if(null == skuPrice || skuPrice.compareTo(BigDecimal.ZERO) == 0) {
                    logsUtil.insertSyncProductLogs(product.getGoodsNo(),
                            product.getGoodsName(), product.getSkuName(), product.getSkuBarcode(),
                            warehouseCode + " " +  "skuPrice为空或者为0",
                            "JKY", product.getCateName(),product.getBrandName(),0);
                    continue;
                }
                skuOtPrice = sku.getPrice();

                LinkedHashMap<String, String> linkedMap = new LinkedHashMap<>();
                List<String> suk = new ArrayList<String>();
                linkedMap.put("颜色", "默认");
                if (!colorValueList.contains("默认")) {
                    colorValueList.add("默认");
                }
                suk.add("默认");
                linkedMap.put("尺寸", skuSize);
                if (!valueList.contains(skuSize)) {
                    valueList.add(skuSize);
                }
                suk.add(skuSize);
                storeProductAttrValue.setSuk(String.join(",", suk));
                storeProductAttrValue.setAttrValue(linkedMap);
                int lockedQuantity = null == sku.getLockedQuantity() ? 0 : Integer.parseInt(sku.getLockedQuantity());
                storeProductAttrValue.setStock(Integer.parseInt(sku.getCurrentQuantity()) - lockedQuantity);
                storeProductAttrValue.setPrice(skuPrice);
                storeProductAttrValue.setImage(skuImage);
                storeProductAttrValue.setCost(skuPrice);
                storeProductAttrValue.setSourceCost(skuPrice);
                storeProductAttrValue.setBarCode(barCode);
                storeProductAttrValue.setOtPrice(priceUtil.rmb2EurStatic(skuOtPrice));
                storeProductAttrValue.setTempId(stockAreaTempEnum.getTempId());
                attrValueList.add(storeProductAttrValue);

                stock += storeProductAttrValue.getStock();
            }
            if(CollectionUtils.isEmpty(attrValueList)) {
                return null;
            }
            // 已售罄
            if(0 >= stock) {
                return null;
            }
            // attr对象
            if (CollectionUtils.isNotEmpty(colorValueList)) {
                storeProductAttrColor.setAttrValues(String.join(",", colorValueList));
                attrList.add(storeProductAttrColor);
            }
            if (CollectionUtils.isNotEmpty(valueList)) {
                storeProductAttr.setAttrValues(String.join(",", valueList));
                attrList.add(storeProductAttr);
            }

            productRequest = new StoreProductCrmebRequest();
//            productRequest.setExchangeRate(new BigDecimal(PriceUtil.DEFAULT_EXCHANGE_RATE_EUR));

            productRequest.setAttr(attrList);
            productRequest.setAttrValue(attrValueList);
            // 强制写一个 商户id 作为buyer商品标识标识
            productRequest.setMerId(CrmebMerchantEnums.CHENGPING.getMerchantId());
            productRequest.setContent(product.getGoodsDesc());
            productRequest.setKeyword(goodsNo);
            productRequest.setStoreInfo(product.getGoodsDesc());
            productRequest.setStoreName(product.getGoodsName().replaceAll("意大利代购", "").replaceAll("（", "").replaceAll("）", "").replaceAll("代购", "").replaceAll("在途", ""));
            if(null != storeBrand) {
                productRequest.setBrandId(storeBrand.getId());
                productRequest.setBrandName(storeBrand.getEnName());
            }
            productRequest.setPrice(price);
            productRequest.setOtPrice(priceUtil.rmb2EurStatic(otPrice));
            productRequest.setCost(price);
            productRequest.setStock(stock);
            productRequest.setSourceCost(price);
            productRequest.setPromoteFlag(false);
            productRequest.setErpPromFlag(true);
            productRequest.setTempId(stockAreaTempEnum.getTempId());

            // 供应商
            Supplier supplier = supplierService.getOne(Wrappers.<Supplier>lambdaQuery()
                    .eq(Supplier :: getBarcode, BarCodeEnum.JKY.getCode())
                    .last("LIMIT 1")
            );
            if(null != supplier) {
                productRequest.setSuppliersId(supplier.getId());
                productRequest.setSuppliersName(supplier.getSuppliersName());
            }
            productRequest.setBarCode(barCode);
            // 分类
            CategoryMatch categoryMatch = categoryMatchService.getOne(Wrappers.<CategoryMatch>lambdaQuery()
                    .eq(CategoryMatch :: getType, CategoryTypeEnum.JKY)
                    .eq(CategoryMatch :: getOthCategoryId, product.getCateId())
                    .eq(!"tfx".equals(warehouseCode), CategoryMatch :: getOthCategoryType, product.getAbcCate())
                    .last("LIMIT 1")
            );
            categoryMatchService.unMatchCategory(categoryMatch,product.getCateId(),product.getAbcCate(),null,CategoryTypeEnum.JKY);
            // 复制重量
            if(null != categoryMatch) {
                CategoryMatch supCategoryMatch = categoryMatchService.getById(categoryMatch.getCategoryId());
                categoryMatch.setWeight(Optional.ofNullable(supCategoryMatch).orElse(new CategoryMatch()).getWeight());
            }
            Integer categoryId = null != categoryMatch ? categoryMatch.getCategoryId() : 1859;
            productRequest.setCateId(categoryId);
            productRequest.setCateIds(Arrays.asList(categoryId));
            productRequest.setWeight(null != categoryMatch ? categoryMatch.getWeight() : BigDecimal.ONE);
            productRequest.setImage(StringUtil.isEmpty(mainImage) ? gallery.getString(0) : mainImage);
            if(gallery.size() > 0) {
                productRequest.setSliderImage(JSON.toJSONString(gallery));
            }
            productRequest.setIsShow(true);
            productRequest.setSpecType(true);
            productRequest.setIsEurope(ProductTypeEnum.JKY.getCode());
            productRequest.setUnitName("件");
            productRequest.setSeasonCode(product.getGoodsAlias());
            productRequest.setColorCode(product.getColorCode());
            productRequest.setLastPushTime(new Date());
            productRequest.setAddTime(Integer.parseInt(String.valueOf(System.currentTimeMillis() / 1000L)));
            // 图片视频归类存储
            productRequest.setGwpp1(null == imageGroup.get(ProductImageEnum.GWPP1.getName()) ? null : JSON.toJSONString(imageGroup.get(ProductImageEnum.GWPP1.getName())));
            productRequest.setGwmt1(null == imageGroup.get(ProductImageEnum.GWMT1.getName()) ? null : JSON.toJSONString(imageGroup.get(ProductImageEnum.GWMT1.getName())));
            productRequest.setSp1(null == imageGroup.get(ProductImageEnum.SP1.getName()) ? null : JSON.toJSONString(imageGroup.get(ProductImageEnum.SP1.getName())));
            productRequest.setSp(null == imageGroup.get(ProductImageEnum.SP.getName()) ? null : JSON.toJSONString(imageGroup.get(ProductImageEnum.SP.getName())));
            productRequest.setMt1(null == imageGroup.get(ProductImageEnum.MT1.getName()) ? null : JSON.toJSONString(imageGroup.get(ProductImageEnum.MT1.getName())));
            productRequest.setMt(null == imageGroup.get(ProductImageEnum.MT.getName()) ? null : JSON.toJSONString(imageGroup.get(ProductImageEnum.MT.getName())));
            productRequest.setMx1(null == imageGroup.get(ProductImageEnum.MX1.getName()) ? null : JSON.toJSONString(imageGroup.get(ProductImageEnum.MX1.getName())));
            productRequest.setSp2(null == imageGroup.get(ProductImageEnum.SP2.getName()) ? null : JSON.toJSONString(imageGroup.get(ProductImageEnum.SP2.getName())));
            productRequest.setSp3(null == imageGroup.get(ProductImageEnum.SP3.getName()) ? null : JSON.toJSONString(imageGroup.get(ProductImageEnum.SP3.getName())));
            BigDecimal exchangeRate = priceUtil.getMoneyExchange(supplier.getCurrencyId());
            productRequest.setExchangeRate(exchangeRate);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return productRequest;
    }
}
